package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;

import com.lottery.ruok.lottrey.retrofitModule.response.GetWholeCategoryResponse;
import com.lottery.ruok.lottrey.ui.component.BuyItemView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by ASUS on 2017/11/13.
 */

public class CategoryAdapter extends BaseListAdapter<GetWholeCategoryResponse.ItemsBean> {

    GetWholeCategoryResponse.AttachedBean attachedBean = null;

    // 保存有倒计时的ViewHolder
    private final SparseArray<BuyItemView> mCountdownArray;

    public CategoryAdapter(Context context) {
        super(context);
        mCountdownArray = new SparseArray<>();
    }

    public CategoryAdapter(Context context, List<GetWholeCategoryResponse.ItemsBean> list) {
        super(context, list);
        mCountdownArray = new SparseArray<>();
    }

    @Override
    protected View createItemView(Context mContext) {
        return new BuyItemView(mContext);
    }

    @Override
    protected void onBindView(View itemView, int position) {
        GetWholeCategoryResponse.ItemsBean data = getList().get(position);
        if (itemView instanceof BuyItemView) {
            BuyItemView view = (BuyItemView) itemView;
            view.setData(data, attachedBean);
        }
        // 处理倒计时, 如果还没有结束
//        if (data.getTime() > 0) {
            mCountdownArray.put(position, (BuyItemView) itemView);
//        }else{ // 如果已经结束
//            mCountdownArray.remove(position);
//        }
    }

    public void setData(GetWholeCategoryResponse data) {
        if (data != null) {
            attachedBean = data.getAttached();
            setList(data.getItems());
        }
    }

    public void refreshTime(){
        int key;
        for (int i = 0; i < mCountdownArray.size(); i++) {
            key = mCountdownArray.keyAt(i);
            BuyItemView itemView = mCountdownArray.get(key);
            itemView.refreshTime();
        }
    }
}
